package appapi.dao;

import appapi.Request.bridge.AccessOperReq;
import appapi.Vo.brider.UserVO;
import appapi.entity.bridge.SceneEntity;
import appapi.entity.bridge.vo.OperatoVo;
import appapi.entity.bridge.vo.ProjectsVo;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;

import appapi.Request.OperPageReq;
import appapi.entity.Zhongjiao.OperatorDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author djq
 * @date 2023/5/25 17:57
 * @description:
 */

@Mapper
public interface OperatorMapper {
    int deleteByPrimaryKey(Long operId);
    @Select("SELECT COUNT(*) FROM operator o " +
            "JOIN project p ON o.projId = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE o.oper_number_type = '注册' AND org.orga_id = #{orgaId}")
    int countRegistered(@Param("orgaId") Integer orgaId);

    @Select("SELECT COUNT(*) FROM operator o " +
            "JOIN project p ON o.projId = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE o.oper_number_type = '场内' AND org.orga_id = #{orgaId}")
    int countOnsite(@Param("orgaId") Integer orgaId);

    @Select("SELECT COUNT(*) FROM operator o " +
            "JOIN project p ON o.projId = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE o.oper_number_type = '访客' AND org.orga_id = #{orgaId}")
    int countVisitor(@Param("orgaId") Integer orgaId);



    int deleteByIds(@Param("operIds") List<Long> operIds);

    int insert(OperatorDto record);

    int insertSelective(OperatorDto record);

    OperatorDto selectByPrimaryKey(Long operId);

    int updateByPrimaryKeySelective(OperatorDto record);

    int updateByPrimaryKey(OperatorDto record);

    List<Map<String, Object>> selectList(long operId);

    List<OperatorDto> selectAllByProjid(@Param("projid") Long projid);

    List<OperatorDto> selectOperList(@Param("projid") Long projid, @Param("name") String name, @Param("operIds") List<Long> operIds, @Param("operItemId") Long operItemId);

    List<Map<String, Object>> selectOperPageList(OperPageReq operPageReq);

    Map<String, Object> selectOperDetail(Long operId);

    OperatorDto selectByIdCard(String operCardId);




    Integer selectCountByMobileAndProjId(@Param("operMobile") String operMobile,@Param("projId") Long projId);

    List<Map<String, Object>> selectOperator();

    List<OperatorDto> selectAllByOperMobile(@Param("operMobile") String operMobile);

    Map<String, Object> selectAllByWxIdAndOperMobile(@Param("wxId") String wxId, @Param("operMobile") String operMobile);


    int updateByOperWxid(@Param("wxId") String wxid, @Param("mobile") String mobile);

    OperatorDto selectAllByOperMobileAndOperName(@Param("operMobile") String operMobile, @Param("operName") String operName);


    @Select("SELECT a.oper_id FROM operator a " +
            "JOIN project b ON a.projId=b.proj_id " +
            "JOIN organization c ON c.orga_id = b.orga_id " +
            "WHERE c.orga_path_ids like CONCAT(#{orgaPath,jdbcType=VARCHAR},'%')")
    List<Map<String, Object>> getOperatorInfoByOrgaId(String orgaPath);

    @Select("SELECT a.oper_id FROM operator a " +
            "JOIN project b ON a.projId=b.proj_id " +
            "WHERE b.proj_id=#{typeId,jdbcType=BIGINT}")
    List<Map<String, Object>> getOperatorInfoByProjId(Long typeId);

    @Select("SELECT a.oper_id FROM operator a " +
            "JOIN project b ON a.projId=b.proj_id " +
            "JOIN equipoperator c ON c.oper_id=a.oper_id " +
            "JOIN equipment d ON d.equip_id=c.equip_id " +
            "WHERE b.proj_id=#{projId,jdbcType=BIGINT} AND d.equip_type_id=#{typeId,jdbcType=BIGINT}")
    List<Map<String, Object>> getOperatorInfoByTypeId(@Param("typeId") Long typeId, @Param("projId") Long projId);

    List<UserVO> getOperator(AccessOperReq req);

    @Select("SELECT o.oper_name AS operName, o.oper_mobile AS operMobile, o.oper_card_id AS operCardId, o.oper_faceimg AS operFaceimg, o.oper_sex AS operSex, o.create_time AS createTime, o.oper_user_id AS operUserId " +
            "FROM operator o " +
            "JOIN project p ON o.projId = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE o.device_sn = #{deviceSn} AND org.orga_id = #{orgaId} " +
            "LIMIT 2")
    List<OperatoVo> getAlllsWithOrgaId(@Param("deviceSn") String deviceSn, @Param("orgaId") Integer orgaId);



    @Select("SELECT s.scene_name AS sceneName, id.device_sn  AS deviceSn " +
            "FROM scene_device sd " +
            "JOIN scene s ON sd.scene_id = s.scene_id " +
            "JOIN iot_device id ON sd.device_id = id.device_id " +
            "JOIN project p ON id.proj_id = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE org.orga_id = #{orgaId}")
    List<ProjectsVo> getSceneNameByOrgaId(@Param("orgaId") Long orgaId);


    List<SceneEntity> selectSceneCheckList(@Param("orgaPathIds") String orgaPathIds);

    List<OperatoVo> selectEquipOperBySceneId(@Param("sceneId") Long sceneId);
}